//自定义
let provinceList = $(".outer .down .item .title");
let mapTip = $(".map-tip");
let mapTitle = $(".map-title");
//地图相关
let map;
let myGeo = new BMapGL.Geocoder();
let adds = [];
let first = true;
let interval;
let labelList = $(".BMapLabel");
let markList = $(".BMap_Marker.BMap_noprint");

$(function () {
    //点击弹出地图标志
    for (let i = 0; i < provinceList.length; i++) {
        $(provinceList[i]).click(function () {
            //自定义样式变化
            let province = $(this).text() + "省";
            mapTip.css("width", "100%");
            mapTip.css("height", "100%");
            first = true;
            setTimeout(function () {
                mapTitle.text("地图标注-" + province);
            }, 500)
            setTimeout(function () {
                //地图数据展示
                map = new BMapGL.Map($('.map-container')[0]);
                //标注省
                myGeo.getPoint(province, function (point) {
                    if (point) {
                        map.centerAndZoom(point, 16);
                        map.addOverlay(new BMapGL.Marker(point, {title: province}))
                    }
                }, province)
                map.enableScrollWheelZoom(true);
                //标注其他地点
                let url = "./data/" + province + "_1.json";
                bdGEO(url);
            }, 500)
        });
    }
    //关闭地图
    mapTitle.click(function () {
        mapTip.css("width", "0%");
        mapTip.css("height", "0%");
        mapTitle.text("");
        first = true;
        clearInterval(interval)
    });
    //标注动态显示
    // for (let i = 0; i < labelList.length; i++) {
    //     $(labelList[i]).mouseenter(function () {
    //         $(this).css("display", "block!important");
    //     });
    //     $(labelList[i]).mouseleave(function () {
    //         $(this).css("display", "none!important");
    //     });
    // }
})


function bdGEO(url) {
    console.log(url);
    //获取数据
    $.get(url, {}, function (result) {
        adds = result;
        let index = 0;
        //展示
        interval = setInterval(function () {
            if (index < adds.length) {
                setTimeout(function () {
                    let add = adds[index];
                    geocodeSearch(add);
                }, 400)
                index++;
            } else {
                clearInterval(interval);
            }
        }, 1000)
    });
}

function geocodeSearch(add) {
    let address = add.province + "省" + add.city;
    if (add.address != null || typeof (add.address) != "undefined") {
        address += add.address;
    }
    console.log(address);
    myGeo.getPoint(address, function (point) {
        if (point) {
            let p = new BMapGL.Point(point.lng, point.lat);
            addMarker(p, new BMapGL.Label(address, {offset: new BMapGL.Size(10, -10)}));
            if (first) {
                first = false;
                map.centerAndZoom(point, 16);
            }
        }
    }, add.province + "省");
}

// 编写自定义函数,创建标注
function addMarker(point, label) {
    let marker = new BMapGL.Marker(point);
    map.addOverlay(marker);
    marker.setLabel(label);
}
